#!/bin/bash
#

#删除所有虚拟机
drop_all_VM(){
    virsh list --all | sed '1,2d'| awk '{print $2}' | while read line
    do
        virsh destroy $line &> /dev/null
        virsh undefine $line &> /dev/null
	rm -rf /var/lib/libvirt/images/*
    done
if [[ $? -eq 0 ]];then
OUT=`cat << @@
    -----------------------------------------
    *	       删除操作执行成功		    *
    -----------------------------------------
@@`
else
OUT=`cat << @@
    -----------------------------------------
    *	       删除操作执行失败		    *
    -----------------------------------------
@@`
fi
}





#删除指定虚拟机
drop_name_VM(){
    read -p "指定虚拟机名称: " vm_name
    status="false"
    
    for line in `virsh list --all | sed '1,2d'| awk '{print $2}'`
    do
        if [[ "$line" == "$vm_name" ]];then
            virsh destroy $vm_name &> /dev/null
            virsh undefine $vm_name &> /dev/null
            rm -rf /var/lib/libvirt/images/${vm_name}.qcow2
            status="true"
        fi
    done
    if [[ $status == "true" ]];then
    OUT=`cat << @@
    -----------------------------------------
    *	        删除操作执行成功	    *
    -----------------------------------------
@@`
    else 
    OUT=`cat << @@
    -----------------------------------------
    *	        删除操作执行失败	    *
    -----------------------------------------
@@`
    fi
}





#批量创建虚拟机
create_VM(){

disk_template=/opt/vm/vm_base.qcow2
config_template=/opt/vm/vm_base.xml

read -p "虚拟机数量: " number

for i in $(seq $number); do
    	vm_name=vm${i}_CentOS79
    #创建每个虚拟机的磁盘文件
    	qemu-img create -f qcow2 -b $disk_template /var/lib/libvirt/images/${vm_name}.qcow2 &> /dev/null
    #创建每个虚拟机的配置文件
    	cp $config_template /etc/libvirt/qemu/${vm_name}.xml
    #修改配置文件
    	sed -ri "s|vm_base|${vm_name}|g" /etc/libvirt/qemu/${vm_name}.xml
    	sed -ri "/uuid/c \  <uuid>$(uuidgen)</uuid>" /etc/libvirt/qemu/${vm_name}.xml

    	mac=52:54:00:$(openssl rand -hex 10 | sed -r 's|(..)(..)(..).*|\1:\2:\3|')
    	sed -ri "/52:54:00/c \      <mac address='${mac}'/>" /etc/libvirt/qemu/${vm_name}.xml

    # 创建虚拟机
    	virsh define /etc/libvirt/qemu/${vm_name}.xml &> /dev/null
done
if [[ -f /opt/vm/vm_base.qcow2 && -f /opt/vm/vm_base.xml ]];then
OUT=`cat << @@
    -----------------------------------------
    *	       批量创建执行成功		    *
    -----------------------------------------
@@`
else
OUT=`cat << @@
    -----------------------------------------
    *            /opt/vm/不存在             * 
    -----------------------------------------
@@`
fi
}






#创建指定虚拟机
create_name_VM(){
    read -p "指定的虚拟机名: " vm_name
    qemu-img create -f qcow2 -b /opt/vm/vm_back.qcow2 /var/lib/libvirt/images/${vm_name}.qcow2 &> /dev/null
    cp /opt/vm/vm_back.xml /etc/libvirt/qemu/${vm_name}.xml
    sed -ri "s|centos_back|${vm_name}|g" /etc/libvirt/qemu/${vm_name}.xml
    sed -ri "/uuid/c \  <uuid>$(uuidgen)</uuid>" /etc/libvirt/qemu/${vm_name}.xml
    random=$(openssl rand -hex 3 | sed -r 's|(..)(..)(..).*|\1:\2:\3|g')
    mac=52:54:00:${random}
    sed -ri "/52:54:00/c \      <mac address='${mac}'/>" /etc/libvirt/qemu/${vm_name}.xml
    virsh define /etc/libvirt/qemu/${vm_name}.xml &> /dev/null 
    if [[ $? -eq 0 ]];then
OUT=`cat << @@
    -----------------------------------------
    *	       创建${vm_name}执行成功	    *
    -----------------------------------------
@@`
else
OUT=`cat << @@
    -----------------------------------------
    *	       创建${vm_name}执行失败	    *
    -----------------------------------------
@@`
fi
}





#修改指定虚拟机CPU
cpu_VM(){
    read -p "虚拟机名称: " vm_name
    cpu_number=$( virsh dominfo ${vm_name} | awk '/CPU：/{print $2}')    
    echo "当前cpu数量: ${cpu_number}
cpu数量可控范围: 1-4 "
    read -p "cpu数量修改为: " number
    virsh setvcpus ${vm_name} ${number} --live
if [[ $? -eq 0 ]];then
cpu_numbers=$( virsh dominfo ${vm_name} | awk '/CPU：/{print $2}')    
OUT=`cat << @@
    -----------------------------------------
    *	    扩容操作执行成功     	    *
    *	    $vm_name cpu数量为:$cpu_numbers *
    -----------------------------------------
@@`
else
OUT=`cat << @@
    -----------------------------------------
    *	    扩容操作执行失败 	            *
    *	    $vm_name cpu数量为:$cpu_number  *
    -----------------------------------------
@@`
fi
}





#修改指定虚拟机内存
mem_VM(){
    read -p "虚拟机名称: " vm_name
    mem_new=`virsh qemu-monitor-command ${vm_name} --hmp info balloon | sed -n 1p | awk -F= '{print $2}'`
    echo "${vm_name}原内存:${mem_new}"
    read -p "修改为多大内存: " mem_number
    virsh qemu-monitor-command ${vm_name} --hmp  balloon $mem_number
if [[ $? -eq 0 ]];then
OUT=`cat << @@
    -----------------------------------------
    *	    修改内存执行成功 	    	    *
    *	    ${vm_name}原内存:${mem_new}     *
    *	    ${vm_name}现内存:${mem_number}  *
    -----------------------------------------
@@`
else
OUT=`cat << @@
    -----------------------------------------
    *	    修改内存执行失败 	    	    *
    *	    ${vm_name}原内存:${mem_new}     *
    *	    ${vm_name}现内存:${mem_number}  *
    -----------------------------------------
@@`
fi
}




#启动指定虚拟机
start_VM() {
    read -p "虚拟机名称: " vm_name
    virsh start ${vm_name}
    if [[ $? -eq 0 ]];then 
OUT=`cat << @@
    -----------------------------------------
    *	    ${vm_name}启动成功 	    	    *
    -----------------------------------------
@@`
    else
OUT=`cat << @@
    -----------------------------------------
    *	    ${vm_name}启动失败 	    	    *
    -----------------------------------------
@@`
    fi
}




#查看现有虚拟机
cat_VM() {
    clear
    cat << eof
------------kvm虚拟机管理工具------------
*       1. 删除所有虚拟机               *
*       2. 删除指定虚拟机               *
*       3. 批量添加虚拟机               *
*       4. 添加单台虚拟机               *
*       5. 扩容指定虚拟机CPU            *
*       6. 修改指定虚拟机的内存         *
*       7. 启动指定虚拟机               *
*       q. 退出                         *
-----------------------------------------
eof
echo
    virsh list --all
echo
echo "$OUT"
OUT=""
}




while true
do
    cat_VM
    read -p "操作: " chooes
    case $chooes in
        1)
          drop_all_VM
        ;;
        2)
          drop_name_VM
        ;;
        3)
     	  create_VM 
        ;;
        4)
          create_name_VM
        ;;
        5)
          cpu_VM
        ;;
        6)
          mem_VM
        ;;
        7)
          start_VM
        ;;
        q)
          break;
        ;;
	*)
	 echo "操作编号有误!! 请重新输入!!!"
        ;;
    esac
done

